'use strict';
const mongoose = require('mongoose');
module.exports = {
    // Notice列表
    list: async function (options) {
        let NoticeModel = mongoose.models['Notice'];
        let notices = await NoticeModel.find({});
        if (!notices || notices.length < 1) {
            await NoticeModel.create({
                title: 'Test Notices',
                aboutImageUrl: 'IMG10010.jpg',
                content: 'Just for test notices ~',
                createTimeTick: Date.now(),
                status: 0
            });
            notices = await NoticeModel.find({});
        }
        return {notices:notices};
    },

    // 添加列表
    add: async function (options) {
        let NoticeModel = mongoose.models['Notice'];
        await NoticeModel.create({
            title: options.noticeTitle,
            aboutImageUrl: options.noticeImgAddress,
            content: options.noticeContent,
            createTimeTick: Date.now(),
            status: parseInt(options.noticeStatus)
        });
        let notices = await NoticeModel.find({});
        return {notices: notices};
    },

    // 修改Notice
    edit: async function (options) {
        let NoticeModel = mongoose.models['Notice'];
        let notice = await NoticeModel.findById(options.ntc_id);
        if (!notice) {
            throw new Error('指定修改的Notice不存在');
        } else {
            notice.title = options.noticeTitle;
            notice.aboutImageUrl = options.noticeImgAddress;
            notice.content = options.noticeContent;
            notice.createTimeTick = Date.now();
            notice.status = parseInt(options.noticeStatus);
            await notice.save();
            return {notice: notice};
        }
    },

    // 删除Notice
    del: async function (options) {
        let NoticeModel = mongoose.models['Notice'];
        let tRes = await NoticeModel.deleteOne({_id: options.ntc_id});
        return {result:tRes};
    },

};